package cmd

import (
	"github.com/spf13/cobra"
	"jenkin/websocket-go/app"
	"log"
	"os"
)

var port string

var rootCmd = &cobra.Command{
	Use:   "server",
	Short: "-v",
	Long:  `websocket-go`,
	Run: func(cmd *cobra.Command, args []string) {
		usageStr := `欢迎使用中websocket-go，使用-h查看命令`
		log.Printf("%s\n", usageStr)
		startServer(args)
	},
}

func init() {
	rootCmd.Flags().StringVarP(&port, "port", "p", "80", "set port")
}

func Execute() {
	if err := rootCmd.Execute(); err != nil {
		log.Println(err)
		os.Exit(-1)
	}
}

func startServer(args []string) {
	router := app.Init()
	// 启动服务器，监听端口
	log.Printf("监听%s端口", port)
	if err := router.Run(":" + port); err != nil {
		log.Fatal("Failed to start server:", err)
	}
}
